﻿Imports Telerik.Windows.Controls
Imports Telerik.Windows.Data
Imports CRM.BusinessLayer
Imports System.Data
Imports System.Text
Partial Public Class frmAdminWhereHear
    Private intSaveNeworEdit As Integer
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmAdminWhereHear_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        refreshList()
        Me.txtWhereHear.IsEnabled = False
        Me.radNo.IsEnabled = False
        Me.radYes.IsEnabled = False
        Me.btnSave.IsEnabled = False
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
    End Sub

    Private Sub refreshList()
        Me.grdWhereHear.ItemsSource = CommonDataFunctions.WhereHearList
        Me.grdWhereHear.Columns(0).IsVisible = False
        Me.grdWhereHear.Columns(1).Width = New GridLength(140)
        'Me.grdQualifications.Columns(3).IsVisible = False
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNew.Click
        intSaveNeworEdit = 1
        ClearControls()
        Me.txtWhereHear.IsEnabled = True
        Me.radNo.IsEnabled = True
        Me.radYes.IsEnabled = True
        Me.btnAddNew.IsEnabled = False
        Me.btnSave.IsEnabled = True
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
        Me.txtWhereHear.Focus()
    End Sub

    Private Sub ClearControls()
        Me.txtWhereHear.Text = String.Empty
        Me.radYes.IsChecked = False
        Me.radNo.IsChecked = False
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        intSaveNeworEdit = 0
        ClearControls()
        Me.txtWhereHear.IsEnabled = False
        Me.radYes.IsEnabled = False
        Me.radNo.IsEnabled = False
        Me.btnAddNew.IsEnabled = True
        Me.btnSave.IsEnabled = False
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
        Me.grdWhereHear.IsEnabled = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click
        intSaveNeworEdit = 2
        'ClearControls()
        Me.txtWhereHear.IsEnabled = True
        Me.radYes.IsEnabled = True
        Me.radNo.IsEnabled = True
        Me.btnSave.IsEnabled = True
        Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
        Me.txtWhereHear.Focus()
    End Sub

    Private Sub grdWhereHear_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles grdWhereHear.SelectionChanged

        If Me.grdWhereHear.SelectedItems.Count = 0 Then
            Me.grdWhereHear.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            Me.btnCancel.IsEnabled = False
            Me.btnDelete.IsEnabled = False
            Exit Sub
        Else
            'Me.grdQualifications.IsEnabled = False
            Me.btnEdit.IsEnabled = True
            Me.btnCancel.IsEnabled = True
            Me.btnDelete.IsEnabled = True
            Me.txtWhereHear.IsEnabled = False
            Me.radYes.IsEnabled = False
            Me.radNo.IsEnabled = False
            Dim selectedRow As DataRow = grdWhereHear.SelectedItem
            If Not IsNothing(selectedRow) Then
                txtWhereHear.Text = selectedRow(1).ToString()
                If selectedRow(2).ToString().ToUpper = "YES" Then
                    radYes.IsChecked = True
                    radNo.IsChecked = False
                ElseIf selectedRow(2).ToString().ToUpper = "NO" Then
                    radNo.IsChecked = True
                    radYes.IsChecked = False
                End If
            End If
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        Dim intWebsiteVisibility As Integer
        If txtWhereHear.Text.Trim = String.Empty Then
            MsgBox("Please enter Where Hear.")
            txtWhereHear.Focus()
            Exit Sub
        ElseIf radYes.IsChecked = False And radNo.IsChecked = False Then
            MsgBox("Please select Option for Visibility on Website !")
            Exit Sub
        Else
            btnSave.IsEnabled = False
            If radYes.IsChecked = True Then
                intWebsiteVisibility = 1
            ElseIf radNo.IsChecked = True Then
                intWebsiteVisibility = 0
            End If
            Dim Sql As New StringBuilder
            Select Case intSaveNeworEdit
                Case 1 'new one
                    Sql = New StringBuilder
                    With Sql
                        .Append("")
                        .Append("insert into l_wherehear ")
                        .Append("  (WhereHear_Text,WhereHear_Website)")
                        .Append(" Values(")
                        .Append(" '" & Me.txtWhereHear.Text & "',")
                        .Append(intWebsiteVisibility & ")")
                    End With

                    If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                        refreshList()
                        MsgBox("The new Where Hear Value has been added successfully.", MsgBoxStyle.Information, "Success")
                        'Exit Sub
                    Else
                        MsgBox("There is an error adding the Where Hear to the system. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                        'Exit Sub
                    End If
                Case 2 'existing one
                    Sql = New StringBuilder
                    With Sql
                        .Append("update l_wherehear set ")
                        .Append(" WhereHear_Text = '" & Me.txtWhereHear.Text & "',")
                        .Append(" WhereHear_Website = " & intWebsiteVisibility)
                        Dim selectedRow As DataRow = grdWhereHear.SelectedItem
                        .Append(" where WhereHear_ID = " & selectedRow(0))
                    End With

                    If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                        refreshList()
                        MsgBox("The Where Hear's details have been updated successfully.", MsgBoxStyle.Information, "Success")
                        'Exit Sub
                    Else
                        MsgBox("There is an error in editing the Where Hear's details. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                        'Exit Sub
                    End If

            End Select


        End If
        refreshList()
        ClearControls()
        txtWhereHear.IsEnabled = False
        radYes.IsEnabled = False
        radNo.IsEnabled = False
        grdWhereHear.IsEnabled = True
        btnCancel.IsEnabled = False
        btnAddNew.IsEnabled = True
        btnEdit.IsEnabled = False
        btnCancel.IsEnabled = False
        'End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
        Dim deleteOption As MsgBoxResult
        deleteOption = MsgBox("Are you really sure to delete This Value of Where Hear ?", MsgBoxStyle.OkCancel)
        If deleteOption = MsgBoxResult.Ok Then
            Dim selectedRow As DataRow = grdWhereHear.SelectedItem
            Dim Sql As New StringBuilder
            With Sql
                .Append("delete  from l_wherehear where intWebsiteVisibility=" + selectedRow(0).ToString())
            End With

            If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                btnDelete.IsEnabled = False
                btnCancel.IsEnabled = False
                btnEdit.IsEnabled = False
                btnSave.IsEnabled = False
                grdWhereHear.IsEnabled = True
                ClearControls()
                refreshList()
                MsgBox("The Where Hear Details have been deleted successfully.", MsgBoxStyle.Information, "Success")
            Else
                MsgBox("There is an error while deleting the Where Hear from the system. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                'Exit Sub
            End If

        ElseIf deleteOption = MsgBoxResult.Cancel Then

        End If

    End Sub
End Class
